<?php

class Item_Model extends Model
{
	public function __construct()
	{
		parent::Model();
		
		// open the database
		$this->load->database();
	}

	function add_item($name, $cat)
	{
		$sql = "INSERT INTO item (item_name, category)
				VALUES (".$this->db->escape($name).", ".$this->db->escape($cat).")";
		
		$result = $this->db->query($sql);
		
		return mysql_insert_id();
	}

	// not sure if we are going toi use it here
	function delete_item($id)
	{
		$result=$this->db->query('DELETE FROM item 
							WHERE item_id = \''.$id.'\'');
	}

	function edit_description($item_id, $heading, $description)
	{
		$data = array(
               'title' => $heading,
               'description' => $description,
            );

		$this->db->update('item', $data,  array('item_id' => $item_id)); 
	}
	
	function get_item_information($id)
	{
		$result = $this->db->query('SELECT item_id, item_name, category, title, description
									FROM item 
									WHERE item_id = \''.$id.'\'');

		return $result->result();
	}
	
	function search_item($filename,$category)
	{
		$id=0;

		// finding the id
		if(strcmp($category," ")==0)
		{
			$result = $this->db->query('SELECT item_id, item_name, category, title, description
								FROM item 
								WHERE item_name like \'%'.$filename.'%\'' );
		}		
		else if((strcmp($category," ")!=0) && (strcmp($filename,"")!=0))
		{
			$result = $this->db->query('SELECT item_id, item_name, category, title, description
								FROM item 
								WHERE item_name like \'%'.$filename.'%\' AND  category like \'%'.$category.'%\'');
		}
		else if((strcmp($category," ")!=0) && (strcmp($filename,"")==0))
		{
			$result = $this->db->query('SELECT item_id, item_name, category, title, description
								FROM item 
								WHERE category like \'%'.$category.'%\'');
		}
		
		return $result->result();
	}
}
?>